package com.mxpio.erp.inventory.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mxpio.erp.common.inventory.entity.*;
import com.mxpio.erp.common.inventory.service.*;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpioframework.jpa.JpaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import javax.validation.constraints.Max;

@Tag(name = "InvFlowController", description = "库存流水接口")
@RestController
@RequestMapping("/erp/inventory/flow/")
public class InvFlowController {
	
	@Autowired
	private InvFlowService invFlowSerivce;
	
	@Autowired
	private VInvLotStockService vInvLotStockService;

	@Autowired
	private VInvStockHoldService vInvStockHoldService;

	@Autowired
	private VItemConsumedByDeptPerMonthService vItemConsumedByDeptPerMonthService;

	@Autowired
	private VItemShortageWarningService vItemShortageWarningService;
	@GetMapping("list")
	@Operation(summary = "库存流水列表", description = "获取库存流水列表", method = "GET")
	public Result<List<InvFlow>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<InvFlow> invFlows = invFlowSerivce.list(InvFlow.class, criteria);
		return Result.OK(invFlows);
	}
	
	@GetMapping("page")
	@Operation(summary = "库存流水列表", description = "获取库存流水列表", method = "GET")
	public Result<Page<InvFlow>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<InvFlow> page = invFlowSerivce.listPageWithRL(InvFlow.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("lot/sum/list")
	@Operation(summary = "批次库存汇总表", description = "批次库存汇总表", method = "GET")
	public Result<List<VInvLotStock>> sumListByLot(Criteria criteria) throws UnsupportedEncodingException {
		List<VInvLotStock> invFlows = vInvLotStockService.listByLot(criteria);
		return Result.OK(invFlows);
	}
	
	@GetMapping("lot/sum/page")
	@Operation(summary = "批次库存汇总表（分页）", description = "批次库存汇总表（分页）", method = "GET")
	public Result<Page<VInvLotStock>> sumPageByLot(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<VInvLotStock> page = vInvLotStockService.listPageByLot(pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("sum/list")
	@Operation(summary = "库存汇总表", description = "库存汇总表", method = "GET")
	public Result<List<VInvLotStock>> sumList(Criteria criteria) throws UnsupportedEncodingException {
		List<VInvLotStock> invFlows = vInvLotStockService.list(criteria);
		return Result.OK(invFlows);
	}
	
	@GetMapping("sum/page")
	@Operation(summary = "库存汇总表（分页）", description = "库存汇总表（分页）", method = "GET")
	public Result<Page<VInvLotStock>> sumPage(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<VInvLotStock> page = vInvLotStockService.listPage(pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("list/{id}")
	@Operation(summary = "根据id获取库存流水", description = "根据id获取库存流水", method = "GET")
	public Result<InvFlow> getById(@PathVariable(name = "id", required = true) String id) {
		InvFlow invFlow = invFlowSerivce.getById(InvFlow.class, id);
		return Result.OK(invFlow);
	}

	@GetMapping("hold/list")
	@Operation(summary = "实时库存(带占用)表", description = "实时库存(带占用)", method = "GET")
	public Result<List<VInvStockHold>> holdList(Criteria criteria) throws UnsupportedEncodingException {
		List<VInvStockHold> list = vInvStockHoldService.list(criteria);
		return Result.OK(list);
	}

	@GetMapping("hold/page")
	@Operation(summary = "实时库存(带占用)表（分页）", description = "实时库存(带占用)表（分页）", method = "GET")
	public Result<Page<VInvStockHold>> holdPage(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<VInvStockHold> page = vInvStockHoldService.listPage(pageAble, criteria);
		return Result.OK(page);
	}

	@GetMapping("itemConsumedByDeptPerMonth/list")
	@Operation(summary = "部门月度物资消耗明细", description = "部门月度物资消耗明细", method = "GET")
	public Result<List<VItemConsumedByDeptPerMonth>> itemConsumedByDeptPerMonthList(Criteria criteria) throws UnsupportedEncodingException {
		List<VItemConsumedByDeptPerMonth> list = vItemConsumedByDeptPerMonthService.list(criteria);
		return Result.OK(list);
	}

	@GetMapping("itemConsumedByDeptPerMonth/page")
	@Operation(summary = "部门月度物资消耗明细（分页）", description = "部门月度物资消耗明细（分页）", method = "GET")
	public Result<Page<VItemConsumedByDeptPerMonth>> itemConsumedByDeptPerMonthPage(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<VItemConsumedByDeptPerMonth> page = vItemConsumedByDeptPerMonthService.listPage(pageAble, criteria);
		return Result.OK(page);
	}

	@GetMapping("itemShortageWarning/list")
	@Operation(summary = "库存安全数量预警明细", description = "库存安全数量预警明细", method = "GET")
	public Result<List<VItemShortageWarning>> itemShortageWarningList(Criteria criteria) throws UnsupportedEncodingException {
		List<VItemShortageWarning> list = vItemShortageWarningService.list(criteria);
		return Result.OK(list);
	}

	@GetMapping("itemShortageWarning/page")
	@Operation(summary = "库存安全数量预警明细（分页）", description = "库存安全数量预警明细（分页）", method = "GET")
	public Result<Page<VItemShortageWarning>> itemShortageWarningPage(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<VItemShortageWarning> page = vItemShortageWarningService.listPage(pageAble, criteria);
		return Result.OK(page);
	}
}
